package br.com.dallasmotopecas.util.data;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class ManipulacaoData
{	
	public static Date getDataHoraAtual()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getDataAtual()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getPrimeiroDiaUtilSemana()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int diaSemana, dia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			
			diaSemana = calendar.get(Calendar.DAY_OF_WEEK);
			dia = calendar.get(Calendar.DAY_OF_MONTH);
			
			if (diaSemana == 1)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 1);
			
			if (diaSemana == 7)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 2);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			calendar.set(Calendar.DAY_OF_WEEK, 2);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getPrimeiroDiaUtilSemanaDataCorrente()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int diaSemana, dia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			
			diaSemana = calendar.get(Calendar.DAY_OF_WEEK);
			dia = calendar.get(Calendar.DAY_OF_MONTH);
			
			if (diaSemana == 1)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 1);
			
			if (diaSemana == 7)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 2);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getUltimoDiaUtilSemanaDataCorrente()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int diaSemana, dia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			
			diaSemana = calendar.get(Calendar.DAY_OF_WEEK);
			dia = calendar.get(Calendar.DAY_OF_MONTH);
			
			if (diaSemana == 1)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 1);
			
			if (diaSemana == 7)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 2);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			calendar.set(Calendar.DAY_OF_WEEK, 6);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getPrimeiroDiaMes(int mes)
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int primeiroDia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, mes);
			
			primeiroDia = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), primeiroDia, 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getPrimeiroDiaMes(int mes, int ano)
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int primeiroDia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, mes);
			calendar.set(Calendar.YEAR, ano);
			
			primeiroDia = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), primeiroDia, 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getUltimoDiaMes(int mes)
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int ultimoDia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, mes);
			
			ultimoDia = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), ultimoDia, 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static Date getUltimoDiaMes(int mes, int ano)
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int ultimoDia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, mes);
			calendar.set(Calendar.YEAR, ano);
			
			ultimoDia = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), ultimoDia, 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static int getMesCorrente()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int mes = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			
			mes = calendar.get(Calendar.MONTH);
			
			return mes;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return 0;
	}
	
	public static int getAnoCorrente()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int ano = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			
			ano = calendar.get(Calendar.YEAR);
			
			return ano;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return 0;
	}
	
	public static Date getProximoDiaSemana()
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int diaSemana, dia = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(new Date());
			calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
			
			diaSemana = calendar.get(Calendar.DAY_OF_WEEK);
			
			dia = calendar.get(Calendar.DAY_OF_MONTH);
			
			if (diaSemana == 1)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 1);
			
			if (diaSemana == 7)
				calendar.set(Calendar.DAY_OF_MONTH, dia + 2);
			
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			calendar.set(Calendar.DAY_OF_WEEK, 2);
			
			return calendar.getTime();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return new Date();
	}
	
	public static int getSemana(Date data)
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int semana = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(data);
			
			semana = calendar.get(Calendar.WEEK_OF_MONTH);
			
			return semana;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return 0;
	}
	
	public static int getDiaSemana(Date data)
	{
		Calendar calendar = null;
		Locale ptBR = null;
		int diaSemana = 0;
		try
		{
			ptBR = new Locale("pt", "BR");
			calendar = Calendar.getInstance(ptBR);
			calendar.setTime(data);
			
			diaSemana = calendar.get(Calendar.DAY_OF_WEEK);
			
			return diaSemana;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (calendar != null)
				calendar = null;
		}
		return 0;
	}
}
